package com.unknowpupil.web.component.wallet.scheduled;

import com.unknowpupil.web.component.qrzt.BaseSchedulerJop;
import com.unknowpupil.web.component.wallet.service.CoinToolsService;
import com.unknowpupil.web.domain.BlockHeight;
import com.unknowpupil.web.service.IBlockHeightService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Classname: com.unknowpupil.web.component.wallet.scheduled.BlockHeightScheduled
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/11/7 14:34
 * @Description:  区块高度轮询
 * @Version: 1.0
 */
@Component
public class BlockHeightScheduled  implements BaseSchedulerJop {
    private static final Logger logger = LoggerFactory.getLogger(BlockHeightScheduled.class);
    @Autowired
    IBlockHeightService blockHeightService;

    @Autowired
    CoinToolsService coinToolsService;
    /**
     * 入金監聽 区块增加
     * 15秒一次
     */
    @Override
    public void  execute(JobExecutionContext context) throws JobExecutionException {
        logger.info("入金任务{}-> 任务开始",context);
        //最新的区块
        try {
            int blockHeight = coinToolsService.getBlockHeight();
            //当前区块
            BlockHeight eth = blockHeightService.getCurrencyBolckHeight("ETH");
            if(blockHeight>eth.getHeight()){
                boolean b = coinToolsService.parseBlock(eth.getHeight());
                if(b){
                    //存区块
                    eth.setHeight(eth.getHeight()+1);
                    blockHeightService.save(eth);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




}
